package com.hup.utils.swing;

import java.awt.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author hugan
 * @date 2020/4/14
 */
public class ColorUtil {
    public static final Color DEF_MOUSE_IN_COLOR = new Color(214, 228, 239);
    public static final Color DEF_SELECTED_COLOR = new Color(172, 208, 255);

    /**
     * 获取反色
     */
    public static Color reverse(Color color) {
        return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue(), color.getAlpha());
    }

    public static int getColor(int a, int r, int g, int b) {
        return ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF));
    }

    public static Color random() {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        return new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
    }

    public static String getIntText(Color color) {
        return getIntText(color.getRGB());
    }

    public static String getIntText(int argb) {
        int a = (argb >> 24) & 0xff;
        int r = (argb >> 16) & 0xff;
        int g = (argb >> 8) & 0xff;
        int b = (argb) & 0xff;
        if (a == 255) {
            return r + "," + g + "," + b;
        } else {
            return a + "," + r + "," + g + "," + b;
        }
    }

    public static String getHexText(Color color) {
        return getHexText(color.getRGB());
    }

    public static String getHexText(int argb) {
        int a = (argb >> 24) & 0xff;
        int r = (argb >> 16) & 0xff;
        int g = (argb >> 8) & 0xff;
        int b = (argb) & 0xff;
        StringBuilder sb = new StringBuilder(a == 255 ? 6 : 8);
        if (a != 255) {
            String aStr = Integer.toHexString(a).toUpperCase();
            if (aStr.length() == 1) sb.append('0');
            sb.append(aStr);
        }

        String rStr = Integer.toHexString(r).toUpperCase();
        String gStr = Integer.toHexString(g).toUpperCase();
        String bStr = Integer.toHexString(b).toUpperCase();
        if (rStr.length() == 1) sb.append('0');
        sb.append(rStr);
        if (gStr.length() == 1) sb.append('0');
        sb.append(gStr);
        if (bStr.length() == 1) sb.append('0');
        sb.append(bStr);
        return sb.toString();
    }

}
